package com.health.work.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.health.work.R;
import com.health.work.utils.AsyncImageLoader;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

public class ProductDetailsActivity extends Activity {

	private ImageView pic;
	private TextView name;
	private TextView price;
	private TextView zan;
	private TextView order;
	private TextView pingzhi;
	private TextView zanBtn;
	private TextView orderBtn;
//	private TextView att;
	private Button back;
	private final static int SUCCESSPRA = -3;
	private final static int AGAIN = -2;
	private final static int FAILEPRA = -1;
	private final static int SUCCESSORDER = 0;
	private final static int FAILEORDER = 1;
	private final static int ERROR = 2;
	private static final int NODATA = 3;
	private final static int SUCCESSATT = 4;
	private final static int FAILEATT = 5;
	private final static int ATTAGAIN = 6;
	private String mobilePhoneNum;
	private Thread thread;

	private String PreOrder;
	private String GoodCount;
	private String Quality;
	private String Price;
	private String Photo;
	private String Name;
	private String Id;
	private AsyncImageLoader asyncImageLoader;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESSORDER:
				Toast.makeText(ProductDetailsActivity.this, "预定成功",
						Toast.LENGTH_SHORT).show();
				break;
			case FAILEORDER:
				Toast.makeText(ProductDetailsActivity.this, "预定失败",
						Toast.LENGTH_SHORT).show();
				break;
			case AGAIN:
				Toast.makeText(ProductDetailsActivity.this, "已经赞过，不能再赞了",
						Toast.LENGTH_SHORT).show();
				break;
			case FAILEPRA:
				Toast.makeText(ProductDetailsActivity.this, "赞美失败",
						Toast.LENGTH_SHORT).show();
				break;
			case SUCCESSPRA:
				Toast.makeText(ProductDetailsActivity.this, "赞美成功",
						Toast.LENGTH_SHORT).show();
				zan.setText("商品赞数：" + (Integer.parseInt(GoodCount)+1)+"");
				break;
			case ERROR:
				TipsUtil.showAlert(ProductDetailsActivity.this, null, "未知错误！");
				break;
			case NODATA:
				TipsUtil.showAlert(ProductDetailsActivity.this, null, "暂无数据！");
				break;
			case SUCCESSATT:
				Toast.makeText(ProductDetailsActivity.this, "关注成功",
						Toast.LENGTH_SHORT).show();
				break;
			case FAILEATT:
				Toast.makeText(ProductDetailsActivity.this, "关注失败",
						Toast.LENGTH_SHORT).show();
				break;
			case ATTAGAIN:
				Toast.makeText(ProductDetailsActivity.this, "已经关注过",
						Toast.LENGTH_SHORT).show();
			break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_details_layout);
		mobilePhoneNum = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		initPages();
		setData();
	}

	private void initPages() {
		// TODO Auto-generated method stub
		back = (Button) findViewById(R.id.btnBack);
		pic = (ImageView) findViewById(R.id.touxiang);
		name = (TextView) findViewById(R.id.name);
		pingzhi = (TextView) findViewById(R.id.pingzhi);
		price = (TextView) findViewById(R.id.price);
		zan = (TextView) findViewById(R.id.zan);
		order = (TextView) findViewById(R.id.yuding);
		zanBtn = (TextView) findViewById(R.id.ask_list);
		orderBtn = (TextView) findViewById(R.id.ask);
		zanBtn.setOnClickListener(onClickListener);
		orderBtn.setOnClickListener(onClickListener);
		back.setOnClickListener(onClickListener);
		pic.setOnClickListener(onClickListener);
//		att = (TextView) findViewById(R.id.att);
//		att.setOnClickListener(onClickListener);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			Name = (String) bundle.getString("Name");
			Photo = (String) bundle.getString("Photo");
			Price = (String) bundle.getString("Price");
			Quality = (String) bundle.getString("Quality");
			GoodCount = (String) bundle.getString("GoodCount");
			PreOrder = (String) bundle.getString("PreOrder");
			Id = (String) bundle.getString("Id");

		}

	}

	private void setData() {
		if (Name != null) {
			name.setText("商品名称：" + Name);
		} else {
			name.setText("商品名称：");
		}
		if (Price != null) {
			price.setText("商品价钱：" + Price);
		} else {
			price.setText("商品价钱：");
		}
		if (Quality != null) {
			pingzhi.setText("商品品质：" + Quality);
		} else {
			pingzhi.setText("商品品质：");
		}

		if (GoodCount != null) {
			zan.setText("商品赞数：" +GoodCount);
		} else {
			zan.setText("商品赞数：");
		}
		if (PreOrder != null) {
			order.setText("商品预定数：" + PreOrder);
		} else {
			order.setText("商品预定数：");
		}

		if (Photo != null) {
			asyncImageLoader = new AsyncImageLoader();
			Drawable drawable = asyncImageLoader.loadImageFromUrl(Photo);
			if (drawable != null) {
				pic.setImageDrawable(drawable);
			} else {
				pic.setImageResource(R.drawable.nophoto);
			}
		}
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnBack:
				finish();
				break;
			case R.id.ask:// 预定
				order();
				break;
			case R.id.ask_list:// 赞
				praise();
				break;
			case R.id.touxiang:
				attention();
				break;
			default:
				break;
			}
		}
	};

	private void order() {

		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().AddPreOrder(Id, mobilePhoneNum,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								if ("0".equals(result)) {
									handler.sendEmptyMessage(SUCCESSORDER);
								} else if ("1".equals(result)) {
									handler.sendEmptyMessage(FAILEORDER);
								} else {
									handler.sendEmptyMessage(ERROR);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub

							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub

							}
						});
			}
		});
		thread.start();
	}

	private void praise() {
		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().AddGoodCount(Id, mobilePhoneNum,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								if ("1".equals(result)) {
									handler.sendEmptyMessage(FAILEPRA);
								} else if ("2".equals(result)) {
									handler.sendEmptyMessage(AGAIN);
								} else {
									handler.sendEmptyMessage(ERROR);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub
								if (object != null && !"".equals(object)) {
									handler.sendEmptyMessage(SUCCESSPRA);
								} else {
									handler.sendEmptyMessage(FAILEPRA);
								}
							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub

							}
						});
			}
		});
		thread.start();
	}
	private void attention() {
		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().AddAttentionNum(Id, mobilePhoneNum,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								if ("1".equals(result)) {
									handler.sendEmptyMessage(FAILEATT);
								} else if ("2".equals(result)) {
									handler.sendEmptyMessage(ATTAGAIN);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub

							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub
								if (array.toString() != null
										&& array.length() > 0) {
									handler.sendEmptyMessage(SUCCESSATT);
								}
							}
						});
			}
		});
		thread.start();
	}
	
}
